package eerbaar.v2.app.view;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

public class Squares {

    private static final int HEIGHT = 50;
    private static final int WIDTH = 50;
    private static final int X_COUNT = 10;
    private static final int Y_COUNT = 10;
    private Square highlighted;
    private final List<Square> squares = new ArrayList<Square>();

    public Squares() {
        createSquares();
    }

    private void createSquares() {

        for (int y = 0; y < Y_COUNT; y++) {
            for (int x = 0; x < X_COUNT; x++) {
                Rectangle r = new Rectangle();
                r.setRect(x * WIDTH, y * HEIGHT, HEIGHT, WIDTH);
                Square square = new Square(r, x, y);
                this.squares.add(square);
            }
        }
    }

    public void draw(Graphics2D g) {

        for (Square square : this.squares) {
            square.draw(g);
        }

        if (this.highlighted != null) {
            this.highlighted.highlight(g);
        }
    }

    public Square getHighlighted() {
        return this.highlighted;
    }

    public Square getSquare(Point point) {

        Square result = null;
        for (Square square : this.squares) {

            Rectangle r = square.getRectangle();
            if (r.contains(point)) {

                result = square;
                break;
            }
        }

        return result;
    }

    public Square[] getSquares() {
        return this.squares.toArray(new Square[] {});
    }

    public void setHighlighted(Square highlighted) {
        this.highlighted = highlighted;
    }

}
